﻿Imports Configuration.Templating.Scope
Imports Aricie.DNN.UI.Controls

Namespace Aricie.LuceneSearch.Settings.Templates

    Public Class TemplateScope
        Inherits AricieUserControlBase

        Public Event ScopeChangedEvent(ByVal ea As EventArgs)

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Dim UI = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo()
#If DEBUG Then
            rbLuceneSearchLevel.Visible = True
#Else
            rbLuceneSearchLevel.Visible = false
#End If
            rbLuceneSearchLevel.Enabled = UI.IsSuperUser
            rbHostLevel.Enabled = UI.IsSuperUser
            rbPortalLevel.Enabled = UI.IsSuperUser OrElse UI.IsInRole(DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings().AdministratorRoleName)
            rbModuleLevel.Enabled = True

        End Sub

        Public Function GetScope() As ITemplateProviderScope
            If rbLuceneSearchLevel.Checked Then
                Return New LuceneSearchScope
            End If
            If rbHostLevel.Checked Then
                Return New HostScope()
            End If
            If rbPortalLevel.Checked Then
                Return New PortalScope(DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings.PortalId)
            End If

            Dim ModuleId As Integer = 0
            If (Request.QueryString("mid") IsNot Nothing) Then
                Integer.TryParse(Request.QueryString("mid"), ModuleId)
            End If

            If (Request.QueryString("moduleid") IsNot Nothing) Then
                Integer.TryParse(Request.QueryString("moduleid"), ModuleId)
            End If
            Return New ModuleScope(DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings.PortalId, ModuleId)

        End Function


        Private Sub ScopeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbHostLevel.CheckedChanged, rbModuleLevel.CheckedChanged, rbPortalLevel.CheckedChanged, rbLuceneSearchLevel.CheckedChanged
            RaiseEvent ScopeChangedEvent(New EventArgs)
        End Sub
    End Class
End Namespace